//
//  Dialog.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2019/12/6.
//  Copyright © 2019 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

typealias UpBtnBlock = ()->Void

class Dialog: NSView {
    
    static let shared = Dialog()

    var kDiaWidth     = CGFloat(300)
    var kDHeight      = CGFloat(110)
    
    var actionBtns    = [LLCustomBtn]()
    
    var touchBlock: UpBtnBlock?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var diaWidth:CGFloat? {
        didSet {
            if diaWidth != nil {
                kDiaWidth = diaWidth!
                self.diaView.snp.updateConstraints { (make) in
                    make.width.equalTo(diaWidth!)
                }
            }
            
        }
    }
    
    
    func setupUI() {
        self.addSubview(maskView)
        maskView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        maskView.addSubview(diaView)
        diaView.snp.makeConstraints { (make) in
            make.width.equalTo(kDiaWidth)
            make.height.equalTo(300)
            make.center.equalToSuperview()
        }
        diaView.zh_addBorder(radius: 5, borderColor: nil, borderWidth: 0)
        
        diaView.addSubview(shadowView)
        shadowView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(7, 7, 7, 7))
        }
        
        ///////
        diaView.addSubview(tefTitle)
        tefTitle.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-40)
            make.height.equalTo(30)
            make.left.equalTo(40)
            make.top.equalToSuperview().offset(60)
        }

    }
    
    /////以下定制版更新内容
    lazy var texView: BaseTextView = {
        let view = BaseTextView.init(frame: CGRect.zero)
        return view
    }()
    
    lazy var cacalBtn: ZHButton = {
        let btn = ZHButton()
        btn.title = "取消".localized()
        btn.font = NSFont.systemFont(ofSize: 13)
        btn.zh_color = NSColor.black
        btn.zh_backgroundColor = NSColor.white
        btn.backgroundHoverColor = color_gray(200)
        btn.hasBorder = true
        btn.zh_cornerRadius = 10
        btn.zh_borderWidth = 0.5
        btn.zh_borderColor = color_gray(200)
        
        btn.action = #selector(cacalBtnAction)
        btn.target = self
        
        return btn
    }()
    
    lazy var updataBtn: ZHButton = {
        let btn = ZHButton()
        btn.title = "更新".localized()
        btn.font = NSFont.systemFont(ofSize: 13)
        btn.zh_color = NSColor.white
        btn.zh_backgroundColor = color_main140
        btn.hasBorder = true
        btn.zh_cornerRadius = 10
       
        btn.action = #selector(updateBtnAction)
        btn.target = self
        return btn
    }()
    
    @objc func updateBtnAction() -> () {
        self.dismiss()
        self.touchBlock!()
    }
    
    @objc func cacalBtnAction() -> () {
        self.dismiss()
    }
    
    
    /////以下初始化内容
    lazy var verLab: NSTextField = {
        let view = NSTextField.label(title: "1.0.1"+"版本更新内容".localized(), textColor: NSColor.black, font: NSFont.boldSystemFont(ofSize: 14))
        view.alignment = .left
        return view
    }()
    
   
    
    lazy var tefTitle: NSTextField = {
        let view = NSTextField.label(title: "新版本啦！", textColor: NSColor.black, font: NSFont.systemFont(ofSize: 26))
        view.alignment = .left
        return view
    }()
    
    
    
    lazy var shadowView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        
        view.shadow = NSShadow()
        view.layer?.shadowOffset = NSSize.init(width: 0, height: 0)
        view.layer?.shadowRadius = 2
        view.layer?.shadowOpacity = 1.0
        view.layer?.shadowColor = color_gray(120).withAlphaComponent(0.6).cgColor
        view.layer?.cornerRadius = 6
        view.layer?.borderColor = color_gray(255).cgColor
        view.layer?.borderWidth = 1
        return view
    }()
    
    lazy var diaView: NSView = {
        let view = NSView()
        return view
    }()
    
    lazy var maskView: MDView = {
        let view = MDView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(255,alpha:0.1).cgColor
        return view
    }()
    
    
    func initlize(_ title:String?) {
        self.setupUI()
        
        kDHeight = CGFloat(110)
        let newT = title != nil ? title!:""
        self.tefTitle.stringValue = newT
    }
    
    func addCont(view:NSView,height:CGFloat) {
        kDHeight+=height
        self.shadowView.addSubview(view)
        view.snp.makeConstraints { (make) in
            make.top.equalTo(kDHeight-height)
            make.bottom.equalTo(-20)
            make.width.equalToSuperview().offset(-60)
            make.left.equalTo(40)
        }
    }
    
    lazy var content: DialogCont = {
        let view = DialogCont.init(frame: NSRect.zero)
        return view
    }()
    
    func show(title:String,
              msg:String?,
              confirmBtnTitle:String,
              confirmAction:BtnTapBlock?=nil,
              cacalBtnTitle:String,
              cacalAction:BtnTapBlock?=nil) {
        Dialog.shared.initlize(title)
        Dialog.shared.diaWidth = CGFloat(300)
        if msg != nil {
            let height = msg!.ga_Size(with: 14).height
            self.content.contentLab.stringValue = msg!
            self.content.confirmAction = confirmAction
            self.content.cacalAction = cacalAction
            self.content.confirmBtn.zh_title = confirmBtnTitle
            self.content.cacalBtn.zh_title = cacalBtnTitle
            self.content.contentLab.snp.updateConstraints { (make) in
                make.height.equalTo(height+120)
            }
            Dialog.shared.addCont(view: self.content, height: height+120)
        }else {
            Dialog.shared.addCont(view: NSView(), height:0)
        }
        Dialog.shared.show(inView: nil)
    }
    
    
    func show(inView:NSView?=nil) {
        
        self.diaView.snp.updateConstraints { (make) in
            make.height.equalTo(kDHeight)
        }
        
        var kView:NSView!
        if inView == nil {
            kView = NSApplication.shared.mainWindow?.contentView
        }else {
            kView = inView!
        }
        
        kView.addSubview(self, positioned: NSWindow.OrderingMode.above, relativeTo: inView)
        self.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func dismiss() {
        //self.maskView.removeFromSuperview()
        self.diaView.removeFromSuperview()
        self.content.removeFromSuperview()
        self.removeFromSuperview()
    }
    
    
}

extension Dialog {
    
    func showUpdata(title:String?=nil,version:String?,msg:String?,touchUp:UpBtnBlock? = nil) {
        
        self.setupUI()

        touchBlock = touchUp

        let keyView = NSApplication.shared.mainWindow?.contentView

        let t = title==nil ? "":title!
        let m = msg==nil ? "":msg!
        let v = version==nil ? "":version!

        self.tefTitle.stringValue = t
        self.texView.txView.string = m
        self.verLab.stringValue = v+"版本更新内容".localized()
        self.cacalBtn.title = "取消".localized()
        self.updataBtn.title = "更新".localized()
        
        diaView.addSubview(verLab)
        verLab.snp.makeConstraints { (make) in
            make.top.equalTo(kDHeight)
            make.width.equalToSuperview().offset(-80)
            make.height.equalTo(22)
            make.left.equalTo(43)
        }
        
        diaView.addSubview(texView)
        texView.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-80)
            make.height.equalTo(55)
            make.top.equalTo(verLab.snp.bottom).offset(10)
            make.left.equalTo(43)
        }

        diaView.addSubview(updataBtn)
        updataBtn.snp.makeConstraints { (make) in
            make.width.equalTo(60)
            make.right.equalTo(-40)
            make.height.equalTo(36)
            make.bottom.equalTo(-40)
        }
        
        diaView.addSubview(cacalBtn)
        cacalBtn.snp.makeConstraints { (make) in
            make.width.equalTo(60)
            make.right.equalTo(updataBtn.snp.left).offset(-15)
            make.height.equalTo(36)
            make.bottom.equalTo(-40)
        }
       
        //diaView高度
        diaView.snp.updateConstraints { (make) in
            make.height.equalTo(CGFloat(300))
        }
        
        keyView?.addSubview(self, positioned: NSWindow.OrderingMode.above, relativeTo: keyView)
        self.snp.makeConstraints { (make) in
           make.edges.equalToSuperview()
        }
   }
}
